<template>
  <ContentWrap>
    <template #header>
      <el-space wrap>
        <slot name="expandBtn"></slot>
        <mdp-hi-query
          :column-configs="columnConfigs"
          v-model="hiQueryParams"
          @change="onHiQueryParamsChange"
        />
        <el-button icon="zoom-out" @click="searchReset()" title="重置查询条件并查询" />

        <span v-if="currOpType == 'mng'">


          <el-popover placement="top-start" :title="'选择创建用户故事的方式'" width="300" trigger="hover">
            <el-space wrap>  
                <el-button @click="showImportFromMenuTemplate()" icon="plus">从模板快速导入故事</el-button>  
                <el-button @click="showAdd()" icon="plus">直接创建用户故事</el-button> 
            </el-space>
            <template #reference>
              <el-button v-if="isTaskCenter != '1' && isMy != '1'" type="primary" icon="plus" :title="'新建'+name" plain/>
            </template>
          </el-popover> 
          <el-button
            :disabled="
              disBtn('delBtn') ||
              !checkBtnQx('delBtn', menuDefId) ||
              sels.length === 0 ||
              load.del == true
            "  plain
            type="danger"
            v-loading="load.del"
            @click="batchDel"
            icon="delete"
          />

          <el-button @click="showParentMenu" type="warning" icon="top" title="更换上级"  plain/>

          <el-button @click="showTaskSelect()" type="success" icon="paperclip" title="将需求与任务进行关联"  plain/>
          <el-button  plain :disabled="load.edit"
            @click="loadTasksToXmMenuState"
            icon="histogram"
            title="汇总任务数据到故事，用于进度、统计等"
            v-loading="load.edit"
          /> 
        </span>
        


        <el-button icon="download" @click="export2Excel()" title="导出当前结果数据"  plain />
        <mdp-table-configs :column-configs="columnConfigs" v-model="checkedColumns" /> 
        <el-button @click="$refs['kanbanDlg'].open()" type="primary" plain>板</el-button> 
        
      <mdp-select v-if="simple==undefined"
        itemCode="demandSource"
        v-model="filters.source"
        placeholder="需求来源"
        clearable
      />  
			  <MdpSelectTag v-model="filters.tagIdList" placeholder="标签" multiple/>  
        <span v-if="currOpType == 'select' && multiple == true">
          <el-button 
            :disabled=" sels.length === 0"
            type="primary"
            @click="selectListConfirm"
            icon="check"
            >确认选择</el-button
          >
        </span>
      </el-space>
    </template>
    <el-space wrap>
      <xm-product-select
        ref="xmProductSelect1"
        v-if="(!xmProduct || !xmProduct.id) && (!xmIteration || !xmIteration.id)"
        
        :link-project-id="selProject ? selProject.id : null"
        @change2="onProductSelected"
        :iterationId="xmIteration ? xmIteration.id : null"
        @clear="onProductClearSelect"
      />
      <xm-iteration-select
        v-if="!xmIteration?.id && product?.id "
				v-model="filters.iterationId"
        
        :product-id="product?.id"
        :link-project-id="selProject ? selProject.id : null"
        placeholder="迭代"
        @change2="onIterationSelected"
        @clear="onIterationClearSelect"
      />
      <el-select
        title="按是否加入过迭代进行过滤"
        style="width: 120px"
        v-model="filters.iterationFilterType"
        placeholder="迭代？"
        clearable
      >
        <el-option value="not-join-any-iteration" label="未加入过迭代" />
        <el-option value="join-any-iteration" label="已加入过迭代" />
        <el-option
          value="not-join-curr-iteration"
          :label="'未加入迭代【' + iteration.iterationName + '】'"
          v-if="iteration && iteration.id"
        />
        <el-option
          value="join-curr-iteration"
          :label="'已加入本迭代【' + iteration.iterationName + '】'"
          v-if="iteration && iteration.id"
        />
      </el-select>
      <el-select
        title="按是否分配了任务进行过滤"
        style="width: 120px"
        v-model="filters.taskFilterType"
        placeholder="任务？"
        clearable
      >
        <el-option value="not-join-any-project" label="未分配过任务的需求" />
        <el-option value="join-any-project" label="已分配过任务的需求" />
        <el-option
          value="not-join-curr-project"
          :label="'未分配任务到项目【' + selProject.name + '】'"
          v-if="selProject && selProject.id"
        />
        <el-option
          value="join-curr-project"
          :label="'已分配任务到项目【' + selProject.name + '】'"
          v-if="selProject && selProject.id"
        />
      </el-select> 
      <xm-user-select :xm-product="product" :sel-project="project"
        label="选择责任人"
        v-model="filters.mmUserid"
        :clearable="true"
        placeholder="责任人"
      />
      <mdp-select v-if="simple==undefined"
        v-model="filters.priority"
        placeholder="优先级"
        clearable 
        itemCode="priority"
      /> 
      <el-input
        v-model="filters.menuName"
        style="max-width: 8em"
        placeholder="需求名称查询"
        clearable
      />
      <el-button
        type="primary"
        v-loading="load.list"
        :disabled="load.list == true"
        @click="searchTableDatas"
        plain
        icon="search"
      />
    </el-space>
  </ContentWrap> 
    <el-table @cell-click="cellClick"
      element-loading-text="努力加载中"
      element-loading-spinner="loading"  
      stripe
      fit
      border
      ref="table"
      v-adaptive="{ bottom: 50 }"
      :data="tableDatas"
      current-row-key="menuId"
      row-key="menuId"
      @sort-change="sortChange"
      highlight-current-row
      v-loading="load.list"
      @selection-change="selsChange"
      @row-click="rowClick"
    >
      <el-table-column sortable type="selection" width="40" v-if="currOpType=='mng'||multiple"/>

      <el-table-column prop="menuName" :label="queryScope=='story'?'故事名称':'史诗/特性/故事'" min-width="350">
        <template #default="scope">
          <el-space class="flex itens-center"> 
              <icon icon="ep:promotion" v-if="scope.row.dclass == '1'"/> 
              <icon icon="ep:flag" v-if="scope.row.dclass == '2'"/>  
              <icon icon="ep:document" v-if="scope.row.dclass == '3'"/>  
            <el-link @click="showEdit(scope.row)"
              >{{ scope.row.seqNo }} &nbsp; {{ scope.row.menuName }}
            </el-link>
            <div class="tool-bar">
              <el-button
                @click="copy(scope.row)"
                icon="document-copy"
                circle
                title="复制一行"
              />
            </div>
          </el-space>
        </template>
      </el-table-column>
      <el-table-column
        prop="mmUsername"
        label="负责人"
        min-width="100"
        show-overflow-tooltip
        sortable
      >
        <template #default="scope">
          <xm-user-select
            show-style="tag"
            :xm-product="product" :sel-project="project"
            v-model="scope.row.mmUserid"
            :init-name="scope.row.mmUsername"
            @change2="editSomeFields(scope.row, 'mmUserid', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="productId" label="产品" width="120" show-overflow-tooltip sortable  colPrefix="res."/>

      <el-table-column prop="finishRate" label="进度" width="120" show-overflow-tooltip sortable>
        <template #default="scope">
          <span
            :style="{ borderRadius: '30px', color: scope.row.finishRate >= 100 ? 'green' : 'blue' }"
          >
            {{ (scope.row.finishRate != null ? scope.row.finishRate : 0) + '%' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="80" sortable  itemCode="menuStatus">
        <template #default="scope">
          <mdp-select
            show-style="tag"
            
            itemCode="menuStatus"
            v-model="scope.row.status"
            label="需求状态"
            @change="editSomeFields(scope.row, 'status', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="priority" label="优先级" width="120" sortable itemCode="priority">
        <template #default="scope">
          <mdp-select
            show-style="tag"
            
            itemCode="priority"
            v-model="scope.row.priority"
            placeholder="优先级" 
            @change="editSomeFields(scope.row, 'priority', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="dtype" label="类型" width="120" sortable v-if="false" itemCode="demandType">
        <template #default="scope">
          <mdp-select
            show-style="tag"
            
            itemCode="demandType"
            v-model="scope.row.dtype"
            placeholder="类型" 
            @change="editSomeFields(scope.row, 'dtype', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="source"
        label="来源"
        width="120"
        show-overflow-tooltip
        sortable 
        itemCode="demandSource" 
      >
        <template #default="scope">
          <mdp-select
            show-style="tag"
            
            itemCode="demandSource"
            v-model="scope.row.source"
            placeholder="来源"
            @change="editSomeFields(scope.row, 'source', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="dlvl" label="层次" width="120" sortable itemCode="demandLvl">
        <template #default="scope">
          <mdp-select
            show-style="tag"
            
            itemCode="demandLvl"
            v-model="scope.row.dlvl"
            placeholder="层次" 
            @change="editSomeFields(scope.row, 'dlvl', $event)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="iterationId" label="迭代" width="150" show-overflow-tooltip sortable kbName="iterationName">
        <template #default="scope"> 
            {{ scope.row.iterationName }} 
        </template>
      </el-table-column>

      <el-table-column prop="taskCnt" label="任务数" min-width="100" show-overflow-tooltip sortable>
        <template #header>
          <el-tooltip content="已完成 / 总数 注意：统计包括下级数据"
            ><span>任务数</span></el-tooltip
          >
        </template>
        <template #default="scope">
          <div>{{ scope.row.taskFinishCnt }}/{{ scope.row.taskCnt }}</div>
        </template>
      </el-table-column>

      <el-table-column
        prop="budgetWorkload"
        label="工时"
        min-width="100"
        show-overflow-tooltip
        sortable
      >
        <template #default="scope">
          <span title="实际工时 / 预算工时 或者 (剩余工时+实际工时)"
            >{{ scope.row.actWorkload }} &nbsp;/ &nbsp;{{ scope.row.budgetWorkload }}h
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="bugCnt" label="缺陷数" min-width="100" show-overflow-tooltip sortable>
        <template #header>
          <el-tooltip content="已关闭缺陷数 / 总缺陷数 注意：统计包括下级数据"
            ><span> 缺陷 </span></el-tooltip
          >
        </template>
        <template #default="scope"> {{ scope.row.closedBugs }}/{{ scope.row.bugCnt }} </template>
      </el-table-column>
      <el-table-column prop="tagNames" label="标签" min-width="150" show-overflow-tooltip>
        <template #default="scope">
          <MdpSelectTag showStyle="tag" :init-name="scope.row.tagNames" v-model="scope.row.tagIds" multiple split="," disabled/>
        </template>
      </el-table-column>
      
      <el-table-column prop="proposerName" label="提出人名称" min-width="150" show-overflow-tooltip/>
      <el-table-column prop="startTime" label="开始时间" min-width="150" show-overflow-tooltip  colType="date"/>
      <el-table-column prop="endTime" label="结束时间" min-width="150" show-overflow-tooltip colType="date"/>
      
					<el-table-column prop="actWorkload" label="实际工时"  width="120" show-overflow-tooltip sortable/> 
					
					<el-table-column prop="budgetWorkload" label="预估工时"  width="120" show-overflow-tooltip sortable/> 
      <el-table-column prop="proposerId" label="提出人编号" min-width="150" show-overflow-tooltip/>
      <template v-if="currOpType=='select' && multiple!==true">
        
      <el-table-column label="操作" min-width="100" fixed="right">
        <template #default="scope"> 
          <el-button @click="selectConfirm(scope.row)" type="primary">选择</el-button>
        </template>
      </el-table-column>

      </template>
    </el-table> 
    <el-pagination
      layout="total, sizes, prev, pager, next"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :page-sizes="[10, 20, 50, 100, 500]"
      :current-page="pageInfo.pageNum"
      :page-size="pageInfo.pageSize"
      :total="pageInfo.total"
      style="float: right"
    /> 
  
  <!--编辑 XmMenu xm_project_menu界面-->
  <mdp-dialog ref="kanbanDlg"
    title="看板"  
    fullscreen  
  > 
    <MdpTableKanban @del="(d,cb)=>handleDel(d,0,cb)"
      @plus="(cb)=>showAdd(cb)"
      @edit="(d,cb)=>showEdit(d,cb)"
      v-model="tableDatas" 
      :columnConfigs="columnConfigs"
      itemKey="menuId" 
      :contentFun="(item)=>item.menuName"
    > 
    <template #toolbar="{element}">
      <MdpSelect showStyle="tag" itemCode="menuStatus" disabled v-model="element.status"/>
    </template>
    </MdpTableKanban>
  </mdp-dialog>
  <!--编辑 XmMenu xm_project_menu界面-->
  <mdp-dialog ref="editDialog"
    title="编辑故事" 
    :with-header="false"
    fullscreen 
    append-to-body
    :close-on-click-modal="false"
  > 
  <template #default="{data,dialog}">
    <XmMenuEdit
      :parent-menu="parentMenu"
      :xm-menu="data.formData" 
      @cancel="dialog.close()"
      @submit="data.callback($event)"
      @edit-fields="data.callback($event)"
    />
    </template>
  </mdp-dialog>

  <!--新增 XmMenu xm_project_menu界面-->
  <mdp-dialog ref="addDialog"
    title="新增故事" width="80%"
  >
  <template #default="{visible,data,dialog}">
    <XmMenuAdd
      :visible="visible"
      :parent-menu="parentMenu"
      :xm-menu="data.formData"
      @cancel="dialog.close()"
      @submit="data.callback"
    />
    </template>
  </mdp-dialog>
  <mdp-dialog
    title="需求模板"
    v-model="templateVisible"
    width="80%"
    append-to-body
    :close-on-click-modal="false"
  >
    <xm-menu-tree  subOpType="select"
      multiple isTpl 
      showConfirm
      @cancel="templateVisible = false"
      @select="onSelectedMenuTemplates"
    />
  </mdp-dialog> 
  <mdp-dialog ref="taskSelectDlg"
    title="选中任务" 
    width="80%"
    append-to-body
    :close-on-click-modal="false"
  >
    <XmTaskList
      :xm-product="product"
      :sel-project="selProject"
      query-scope="task" 
      multiple
      @select="onSelectedTasks"
    />
  </mdp-dialog>   
  <mdp-dialog ref="parentDlg"
    append-to-body
    title="选择上级需求" 
    width="60%"
    :close-on-click-modal="false"
  >
    <XmEpicFeatures subOpType="select" showConfirm @select="onParentMenuSelected" :xm-product="product" />
  </mdp-dialog>

  <mdp-dialog ref="funcDlg"
    append-to-body
    title="模块选择" 
    size="40%"
    top="20px"
    :close-on-click-modal="false"
  >
    <xm-func-tree showConfirm
      subOpType="select"
      @select="onFuncSelected"
      :xm-product="xmProductCpd"
    />
  </mdp-dialog>
</template>

<script>
import util from '@/components/mdp-ui/js/util' //全局公共库
import treeTool from '@/components/mdp-ui/js/treeTool' //全局公共库
//import Sticky from '@/components/Sticky' // 粘性header组件

import {
  listXmMenu,
  delXmMenu,
  addXmMenu,
  batchDelXmMenu,
  batchAddXmMenu,
  batchEditXmMenu,
  listXmMenuWithState,
  listXmMenuWithPlan,
  batchChangeParentMenu,
  editXmMenuSomeFields
} from '@/api/xm/core/xmMenu'
import { batchRelTasksWithMenu } from '@/api/xm/core/xmTask'
import { loadTasksToXmMenuState } from '@/api/xm/core/xmMenuState'

import { batchDelXmIterationMenu, batchAddXmIterationMenu } from '@/api/xm/core/xmIterationMenu'

import XmMenuAdd from './XmMenuAdd.vue' //新增界面
import XmMenuEdit from './XmMenuEdit.vue' //修改界面  
import XmTaskList from '../xmTask/XmTaskList.vue'

import XmEpicFeatures from './XmEpicFeatures.vue' 

import { sn } from '@/components/mdp-ui/js/sequence'

import { mapState } from 'pinia'
import { useUserStore } from '@/store/modules/user'

import { MdpTableMixin } from '@/components/mdp-ui/mixin/MdpTableMixin.js'

export default {
  mixins: [MdpTableMixin],
  props: ['xmIteration', 'xmProduct', 'disabledMng', 'parentMenu', 'selProject','simple','queryScope'],//queryScope:'ep/story'
  computed: {
    ...mapState(useUserStore, ['userInfo', 'roles']),
    toSearchCpd() {
      var key = { iterationId: '', projectId: '', productId: '', pmenuId: '' }
      if (this.xmIteration && this.xmIteration.id) {
        key.iterationId = this.xmIteration.id
        key.productId = this.xmIteration.productId
      } else {
        key.iterationId = ''
      }
      if (this.xmProduct && this.xmProduct.id) {
        key.productId = this.xmProduct.id
      } else {
        key.productId = ''
      }
      if (this.selProject && this.selProject.id) {
        key.projectId = this.selProject.id
      } else {
        key.projectId = ''
      }

      // if (this.parentMenu && this.parentMenu.menuId) {
      //   key.pmenuId = this.parentMenu.menuId
      //   key.productId = this.parentMenu.productId
      // } else {
      //   key.pmenuId = ''
      // }
      return key.iterationId + key.projectId + key.productId + key.pmenuId
    },
    xmProductCpd() {
      if (this.product && this.product.id) {
        return this.product
      }
      if (this.xmIteration && this.xmIteration.id) {
        return { id: this.xmIteration.productId, productName: this.xmIteration.productName }
      }
      if (this.parentMenu && this.parentMenu.menuId) {
        return { id: this.parentMenu.productId, productName: this.parentMenu.productName }
      }
      return {}
    }
  },
  watch: {
    xmIteration: function () {
      this.iterationFilterType = 'join-curr-iteration'
      this.iteration = this.xmIteration
    },
    xmProduct: function () {
      this.product = this.xmProduct
    },
    toSearchCpd() {
      this.searchTableDatas()
    }
  },
  data() { 
    return {
      pkNames:['menuId'],
      currOpType: 'mng', 
      filters: {
        key: '',   
        iterationFilterType: '', //join、not-join、''
        taskFilterType: '', //join、not-join、''
        tagIdList: [],
        status: '', 
        dlvl: '',
        dtype: '',
        priority: '',
        source: '',
        menuId: '', //需求编号
        productId: '', //产品编号
        planStartTimeStart: '',
        planStartTimeEnd: '',
        planEndTimeStart: '',
        planEndTimeEnd: '',
        menuName: '',
        dclass:'3'
      }, 
      pageInfo: {
        //分页数据
        total: 0, //服务器端收到0时，会自动计算总记录数，如果上传>0的不自动计算。
        pageSize: 20, //每页数据
        count: true, //是否需要重新计算总记录数
        pageNum: 1, //当前页码、从1开始计算
        orderFields: ['res.ctime'], //排序列 如 ['sex','student_id']，必须为数据库字段
        orderDirs: ['desc'] //升序 asc,降序desc 如 性别 升序、学生编号降序 ['asc','desc']
      }, 
      displayType: 'table', 
      apis: {
        list: listXmMenuWithState,
        add: addXmMenu,
        del: delXmMenu,
        batchDel: batchDelXmMenu,
        editSomeFields: editXmMenuSomeFields
      },
      templateVisible: false,
      product:null,
      project:null,
    }
  }, //end data
  methods: {
    preQueryParamCheck(params) { 
      if (this.iterationFilterType) {
        params.iterationFilterType = this.iterationFilterType 
      } else {
        if (this.iteration) {
          params.iterationId = this.iteration.id
        }
      }
      if (params.iterationFilterType === 'not-join-any-iteration') { 
        delete params.iterationId
      } else if (params.iterationFilterType === 'join-any-iteration') {  
        delete params.iterationId
      } else if (params.iterationFilterType === 'not-join-curr-iteration') {
        params.filterIterationId = this.iteration?.id||this.xmIteration?.id
        delete params.iterationId
      } else if (params.iterationFilterType === 'join-curr-iteration') {
        params.filterIterationId = this.iteration?.id||this.xmIteration?.id
      }
      if (this.xmIteration && this.xmIteration.id) {
        params.linkIterationId = this.xmIteration.id
      }
      if (params.taskFilterType) { 
        if (params.taskFilterType === 'not-join-curr-project') {
          params.projectId = this.selProject.id
        }
        if (params.taskFilterType === 'join-curr-project') {
          params.projectId = this.selProject.id
        } 
      }
      if (this.selProject && this.selProject.id) {
        params.linkProjectId = this.selProject.id
      }
      if (this.product) {
        params.productId = this.product.id
      }
      if (this.parentMenu && this.parentMenu.menuId) {
        params.pmenuId = this.parentMenu.menuId
      }else{
        params.pmenuId=''
      }
      if(this.queryScope=='story'){
        params.dclass=">2"
      } 
      return true
    },

    //显示编辑界面 XmMenu xm_project_menu
    showEdit: function (row,callback) {
      this.$refs['editDialog'].open({formData:row,callback:(f)=>{
        Object.assign(row,f)
        if(callback){
          callback(row)
        }
      }})
    },
    //显示新增界面 XmMenu xm_project_menu
    showAdd: function (callback) {
      
      var formData = {   }
      if (this.product && this.product.id) {
        formData.productId = this.product.id
        formData.productName = this.product.productName
        formData.dclass = '3'
      }
      if (this.iteration && this.iteration.id) {
        formData.productId = this.iteration.productId
        formData.iterationId = this.iteration.id
        formData.iterationName = this.iteration.iterationName
        formData.dclass = '3'
      }
      if(this.parentMenu){
        formData.pmenuId=this.parentMenu.menuId
        formData.productId=this.parentMenu.productId||formData.productId
        formData.productName=this.parentMenu.productName||formData.productName
        formData.iterationId=this.parentMenu.iterationId||formData.iterationId
      }
      if (!formData.productId) { 
        this.$notify({
          position: 'bottom-left',
          showClose: true,
          message: '请先选择一个产品',
          type: 'warning'
        })
        return;
      } 
      this.$refs.addDialog.open({formData:formData,callback:(res)=>{
        
        if(callback){
          callback(res)
        }
        this.searchTableDatas()
      }})
      //formData=Object.assign({}, this.editForm);
    },
    onProductSelected: function (product) {
      this.product = product
      this.filters.iterationId=''
    },
    onProductClearSelect: function () {
      this.product = null
      this.filters.iterationId=''
    },
    onIterationSelected: function (iteration) {
      this.iteration = iteration
    },
    onIterationClearSelect: function () {
      this.iteration = null
    }, 

    showImportFromMenuTemplate() {
      if (!this.product) {
        this.$notify.warning('请选择产品')
        return
      }
      //this.parentMenu=row
      this.templateVisible = true
    },
    onSelectedMenuTemplates: function (menuTemplates) {
      if (menuTemplates == null || menuTemplates.length == 0) {
        this.templateVisible = false
        return
      } 
      if(this.parentMenu?.menuId){
        if(this.parentMenu.dclass=='1'){
          let ms=menuTemplates.filter(k=>k.dclass<='1')
          if(ms.length>0){
            this.$message.error("【"+ms.map(k=>k.menuName).join(",")+"】为史诗，不能导入到同为史诗的【"+this.parentMenu.menuName+"】中，请选中特性或者用户故事进行导入")
            return;
          }
        }else if(this.parentMenu.dclass=='2'){
          let ms=menuTemplates.filter(k=>k.dclass<='2')
          if(ms.length>0){
            this.$message.error("【"+ms.map(k=>k.menuName).join(",")+"】为史诗/特性，不能导入到特性【"+this.parentMenu.menuName+"】中，请选中用户故事进行导入")
            return;
          }
        }else if(this.parentMenu.dclass>'2'){
          this.$message.error("不允许导入任何需求到到故事【"+this.parentMenu.menuName+"】中") 
          return;
        } 
      }

      let msg="将导入"+menuTemplates.length+"个需求到产品【"+this.product.productName+"】中【根目录】下，确认提交吗？"
      if(this.parentMenu?.menuId){
        msg="将导入"+menuTemplates.length+"个需求到产品【"+this.product.productName+"】中的【"+this.parentMenu.menuName+"】下，确认提交吗？"
      }
      this.$confirm(msg, '提示', {}).then(() => {

        var menuTemplates2 = JSON.parse(JSON.stringify(menuTemplates)) 
        let pmenuId=this.parentMenu?.menuId||'0' 
        let productId=this.parentMenu?.menuId?this.parentMenu.productId:this.product.id
        var params={xmMenus:menuTemplates2,pmenuId:pmenuId,productId: productId}
        batchAddXmMenu(params)
          .then((res) => {
            this.templateVisible = false
            this.load.add = false
            var tips = res.tips 
            if(tips.isOk){
              this.$emit('import-template')
            }
              this.$notify({
                position: 'bottom-left',
                showClose: true,
                message: tips.msg,
                type: tips.isOk?'success':'error'
              }) 
          })
          .catch((err) => (this.load.add = false))

      })
     
    },


    onSelectedTasks: function (xmTasks) {
      if (xmTasks == null || xmTasks.length == 0) {
        this.$notify.error('请最少选择一个任务进行关联')
        return
      }

      this.selectTaskVisible = false
      var params = {
        menuId: this.sels[0].menuId,
        taskIds: xmTasks.map((i) => i.id)
      }
      batchRelTasksWithMenu(params).then((res) => {
        var tips = res.tips
        if (tips.isOk) { 
          this.$refs['taskSelectDlg'].close()
        }
        this.$notify({
          position: 'bottom-left',
          showClose: true,
          message: tips.msg,
          type: tips.isOk ? 'success' : 'error'
        })
      })
    },

    loadTasksToXmMenuState: function () {
      if (!this.product) {
        if (!this.iteration) {
          this.$notify.warning('请先选择产品或者迭代')
          return
        }
      }
      this.load.edit = true
      let params = { productId: '' }
      if (!this.product) {
        params.productId = this.iteration.productId
      } else {
        params = { productId: this.product.id }
      }

      loadTasksToXmMenuState(params)
        .then((res) => {
          this.load.edit = false
          var tips = res.tips
          if (tips.isOk) {
            this.pageInfo.count = true 
            this.searchTableDatas()
          }
          this.$notify({
            position: 'bottom-left',
            showClose: true,
            message: tips.msg,
            type: tips.isOk ? 'success' : 'error'
          })
        })
        .catch((err) => (this.load.edit = false))
    },
    onGroupUserSelect(users, option) {
      this.editSomeFields(option.data, 'mmUserid', users)
    },
    showParentMenu() {
      if (this.product && this.product.id) {
        if (this.sels.length == 0) {
          this.$notify({
            position: 'bottom-left',
            showClose: true,
            message: '请先选择一个或者多个需求',
            type: 'warning'
          })
          return
        }
      } else {
        this.$notify({
          position: 'bottom-left',
          showClose: true,
          message: '请先选择产品',
          type: 'warning'
        })
        return
      }
      this.$refs['parentDlg'].open()
    },
    onParentMenuSelected(menu) {
      if (!menu || !menu.menuId) {
        this.$notify({
          position: 'bottom-left',
          showClose: true,
          message: '请先选择一个上级需求',
          type: 'warning'
        })
        return
      } 
      var params =  this.sels.map((i) => { return {menuId:i.menuId,pmenuId:menu.menuId}})
      batchChangeParentMenu(params).then((res) => {
        var tips = res.tips
        if (tips.isOk) {
          this.searchTableDatas()
          this.$refs['parentDlg'].close()
        }
        this.$notify({
          position: 'bottom-left',
          showClose: true,
          message: tips.msg,
          type: tips.isOk ? 'success' : 'error'
        })
      })
    }, 
    calcMenuLabel(dclass) {
      var params = { label: '工作项', icon: '', color: '' }
      if (dclass === '1') {
        params = { label: '史诗', icon: 'promotion', color: 'rgb(255, 153, 51)' }
      } else if (dclass === '2') {
        params = { label: '特性', icon: 'flag', color: 'rgb(0, 153, 51)' }
      } else if (dclass === '3') {
        params = { label: '用户故事', icon: 'document', color: ' rgb(79, 140, 255)' }
      }
      return params
    }, 
    editSomeFieldsCheck(row, fieldName, $event,params) { 
      if (fieldName === 'iterationId') {
        if ($event) {
          params[fieldName] = $event.id
          params.iterationName = $event.iterationName
        } else {
          return
        }
      } else if (fieldName === 'tagIds') {
        if ($event) {
          params[fieldName] = $event.map((i) => i.tagId).join(',')
          params.tagNames = $event.map((i) => i.tagName).join(',')
        } else {
          return
        }
      } else if (fieldName === 'workload') {
        params = { ...params, ...$event }
      } else if (fieldName === 'mmUserid') {
        params.mmUserid = $event.userid
        params.mmUsername = $event.username
      } else {
        params[fieldName] = $event
      }
      return true
    }, 
    showTaskSelect(){
      if(this.sels.length<=0){
        this.$message.error("请先选择1个需要关联任务的用户故事")
        return;
      }
      
      if(this.sels.length>1){
        this.$message.error("一次只能选择1个需要关联任务的用户故事")
        return;
      }
      this.$refs['taskSelectDlg'].open()
    },
    onEditSomeFields(params) {
      Object.assign(this.editForm, params)
    }, 
 
  }, //end methods
  components: {
     XmMenuAdd,
     XmMenuEdit,
       
    XmEpicFeatures, 
    
    XmTaskList 
    //在下面添加其它组件
  },
  mounted() {
    this.product = this.xmProduct
    if (this.xmProduct && this.xmProduct.id) {
      this.productVisible = false
    }

    if (this.xmIteration && this.xmIteration.id) {
      this.filters.iterationFilterType = 'join-curr-iteration'
      this.iteration = this.xmIteration
    }
    this.project=this.selProject
  }
}
</script>

<style lang="scss" scoped> 
</style> 
